package br.com.dispositivosmoveis.acompanhamentodeconta.Adapters;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import br.com.dispositivosmoveis.acompanhamentodeconta.R;
import br.com.dispositivosmoveis.acompanhamentodeconta.model.Item;
import br.com.dispositivosmoveis.acompanhamentodeconta.util.MaskUtil;

public class ItemListAdapter extends ArrayAdapter<Item> {
	Context context;
	List<Item> itens = new ArrayList<Item>();
	Item item;

	public ItemListAdapter(Context context, int layoutResourceId,
			List<Item> itens) {
		super(context, layoutResourceId, itens);
		this.context = context;
		this.itens = itens;
	}

	@Override
	public int getCount() {
		return itens.size(); // total number of elements in the list
	}

	@Override
	public Item getItem(int i) {
		return itens.get(i); // single item in the list
	}

	@Override
	public long getItemId(int i) {
		return i; // index number
	}

	@Override
	public View getView(int position, View convertView, final ViewGroup parent) {
		View row = convertView;
		ItemHolder holder = null;

		if (row == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			row = inflater.inflate(R.layout.row, parent, false);

			holder = new ItemHolder();
			holder.txtDescircao = (TextView) row.findViewById(R.id.txtViewDescricao);
			holder.txtValor = (TextView) row.findViewById(R.id.txtViewValor);

			row.setTag(holder);
		} else {
			holder = (ItemHolder) row.getTag();
		}

		item = itens.get(position);
		holder.txtDescircao.setText(item.getDescricao());
		holder.txtValor.setText(MaskUtil.insertCurrencyMask(item.getValor()));

		return row;

	}

	static class ItemHolder {
		TextView txtDescircao;
		TextView txtValor;
	}
}
